package main

import (
	"fmt"
	"time"
)

type TimeSlice struct {
	StartTime time.Time
	EndTime   time.Time
}

type Item struct {
	slice *TimeSlice
}

// 获取时区配置
func getLoc() *time.Location {
	return time.FixedZone("us", -4*3600)
}

func newTimeSlice(start, end string) (*TimeSlice, error) {
	if start == "" || end == "" || start >= end {
		return nil, fmt.Errorf("时间格式异常")
	}
	dateList := []string{"2023-07-10"}
	for _, trade_date := range dateList {
		// 对应时区交易日当前时间 -> 系统日期
		value := fmt.Sprintf("%s %s", trade_date, time.Now().In(getLoc()).Format("15:04:05"))
		fmt.Println(111111, value)
		t, _ := time.ParseInLocation("2006-01-02 15:04:05", value, getLoc())
		sys_date := t.In(time.Local).Format("2006-01-02")
		fmt.Println(222222, sys_date)
		endAt, err := time.ParseInLocation("2006-01-02 15:04:05", fmt.Sprintf("%s %s", sys_date, end), time.Local)
		if err != nil {
			return nil, err
		}
		if time.Until(endAt) > 0 {
			startAt, err := time.ParseInLocation("2006-01-02 15:04:05", fmt.Sprintf("%s %s", sys_date, start), time.Local)
			if err != nil {
				return nil, err
			}
			return &TimeSlice{
				StartTime: startAt,
				EndTime:   endAt,
			}, nil
		}
	}

	return nil, fmt.Errorf("交易日期异常")
}

func main() {
	timeSlice := make([][]string, 0)
	timeSlice = append(timeSlice, []string{"00:00:00", "04:00:00"})
	timeSlice = append(timeSlice, []string{"15:00:00", "23:00:00"})
	item := Item{}
	for _, v := range timeSlice {
		if slice, err := newTimeSlice(v[0], v[1]); err == nil {
			if item.slice == nil {
				item.slice = slice
			} else if time.Until(slice.StartTime) <= 0 && time.Until(slice.EndTime) > 0 {
				item.slice = slice
				break
			} else if time.Until(slice.StartTime) < time.Until(item.slice.StartTime) {
				item.slice = slice
			}
		}

	}
	fmt.Println(55555555, item.slice)
}
